package com.ruoyi.quartz.controller;

import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.service.ISysJobMyService;
import org.quartz.SchedulerException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.slf4j.Logger;
import com.alibaba.druid.sql.parser.ParserException;

/**
 * 调度任务信息操作处理
 * 
 * @author bocgx
 */
@Component("myTask")
public class SysJobMyController
{
    private final static Logger logger = LoggerFactory.getLogger(SysJobMyController.class);

    @Value("${ruoyi.ftpPath}")
    private  String ftpPath;

    @Autowired
    private ISysJobMyService sysJobMyService;

    /**
     * 新增调度任务信息加载文件更新机构及用户及角色
     * 
     * @param 
     * @return 结果
     * @throws TaskException
     * @throws SchedulerException 
     */
    public void updateDeptAndUser() throws IOException, TaskException, SchedulerException {
//        Path sysDeptPath = Paths.get(ftpPath+"/sys_dept.txt");
//        Path sysUserPath = Paths.get(ftpPath+"/sys_user.txt");
//        try{
//            Files.deleteIfExists(sysDeptPath);
//            Files.deleteIfExists(sysUserPath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        /*
        //支持使用load data infile 方式
        sysJobMyService.updateDeptAndUser();
        sysJobMyService.extractAndLoadSysDeptTableTask();
        sysJobMyService.extractAndLoadSysUserTableTask();
        sysJobMyService.extractAndLoadSysRoleDeptTableTask();
         */

        //不支持使用load data infile 方式
        sysJobMyService.batchDeptImport();
        sysJobMyService.batchUserImport();
        sysJobMyService.extractAndLoadSysDeptTableTask();
        sysJobMyService.extractAndLoadSysUserTableTask();
        sysJobMyService.extractAndLoadSysRoleDeptTableTask();
    }

        public void extractTableTaskByXML() throws IOException, TaskException, SchedulerException {
        sysJobMyService.extractTableTaskByXML();
    }
}
